package com.youdianstar.app.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.youdianstar.app.R
import com.youdianstar.app.databinding.LayoutItemInviteRuleBinding
import com.youdianstar.app.entity.InviteInfo
import com.youdianstar.app.util.DataAppManager
import com.youdianstar.app.util.DataHelperEx


/**
 * File: InvitePlanAdapter
 * Author: Administrator Create: 2024/4/22 0022 16:57
 * Changes (from 2024/4/22 0022)
 * --------------------------------------------------
 * description:
 * ---------------------------------------------------
 *
 */
class InvitePlanAdapter(val mContext: Context, val dataList: List<InviteInfo>) : RecyclerView.Adapter<InvitePlanAdapter.ListHolder>(){
    class ListHolder(val binding : LayoutItemInviteRuleBinding) : RecyclerView.ViewHolder(binding.root)

    var symbolStr = ""
    init {
        symbolStr = DataAppManager.instance.symbolTx()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ListHolder {
        return ListHolder(LayoutItemInviteRuleBinding.inflate(LayoutInflater.from(parent.context), parent, false))
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    override fun onBindViewHolder(holder: ListHolder, position: Int) {
        dataList[position].let { item ->
            val numBg :Int = if (item.inviteeNum>=50) R.mipmap.ic_invite_50x
            else if (item.inviteeNum in 10..49){
                R.mipmap.ic_invite_10x
            }else{
                R.mipmap.ic_invite_1x
            }
            holder.binding.ivSize.setImageResource(numBg)
            holder.binding.tvNum.text = item.inviteeNum.toString()+"x"
//            val titleTx = if (item.inviteeNum>0) String.format(mContext.getString(R.string.invite_ruleText),item.inviteeNum.toString()) else "--"
//            holder.binding.tvTitle.text = titleTx
            if (item.rewardList.isNotEmpty()){
                item.rewardList[0].let { rule ->
                    var pointRs = false
                    var couponRs = false
                    var creditRs = false
                    val buildTx = StringBuilder()
                    rule.type.let { types ->
                        val rewardTypeList = listOf<String>(*types.split(",".toRegex()).toTypedArray())
                        for ((index,typeV) in rewardTypeList.withIndex()){
                            if (index != 0 && typeV != "3"){ //目前没有余额
                                buildTx.append(" & ")
                            }
                            val pendReward = when(typeV){
                                "1"->{
                                    pointRs = true
                                    mContext.getString(R.string.invite_points)+": "+rule.point.toString()
                                }
                                "2"->{
                                    couponRs = true
                                    val numTx :String = if (rule.quantity>1) "*"+rule.quantity.toString() else ""
                                    if (rule.couponInfo!=null){
                                        val deductTitle = if (null!=rule.couponInfo.deduction) DataHelperEx.couponDeductTx(mContext,symbolStr,rule.couponInfo.deduction) else "--"
                                        mContext.getString(R.string.invite_coupon)+": "+deductTitle+ " "+numTx
                                    }else{
                                        mContext.getString(R.string.invite_coupon)+": "+rule.couponTitle+ " "+numTx
                                    }
                                }
                                "3"->{
                                    creditRs = true
                                    //mContext.getString(R.string.invite_credits)+": "+ MathUtil.round(rule.amount)
                                    "" //目前没有余额
                                }
                                else -> ""
                            }
                            buildTx.append(pendReward)
                        }
                    }
                    holder.binding.txNotify.text = buildTx.toString()
                    holder.binding.llPoints.visibility = if (pointRs) View.VISIBLE else View.GONE
                    holder.binding.llCoupon.visibility = if (couponRs) View.VISIBLE else View.GONE
                    holder.binding.llCredits.visibility = View.GONE//if (creditRs) View.VISIBLE else View.GONE
                }
            }else{
                holder.binding.txNotify.text = "--"
                holder.binding.llPoints.visibility = View.INVISIBLE
                holder.binding.llCoupon.visibility = View.INVISIBLE
                holder.binding.llCredits.visibility = View.INVISIBLE
            }
        }
    }
}